package com.yz.lzq.liwushuo.ui.modules.category.danping;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;

import com.yz.lzq.liwushuo.jsonbean.category.CategroyDanPBean;
import com.yz.lzq.liwushuo.ui.modules.category.gongl.CategoryListViewAdapter;

import java.util.List;

/**
 * Created by AdminJia on 2016/11/29.
 */

public class CategoryDanPPresenter {
    private ICategoryDanPBiz iCategoryDanPBiz;
    private ICategoryDanPView iCategoryDanPView;

    public CategoryDanPPresenter(ICategoryDanPView iCategoryDanPView) {
        this.iCategoryDanPBiz = new CategoryDanPBiz();
        this.iCategoryDanPView = iCategoryDanPView;
    }

    public void loadData(final Context context) {
        iCategoryDanPBiz.getCategoryData(new OnCategoryDanPListener() {
            @Override
            public void getDataSuccess(List<Object> list_object) {
                GridLayoutManager manager = new GridLayoutManager(context, 3);
                //GridLayoutManager.SpanSizeLookup()
                final CategoryDanPAdapter adapter = new CategoryDanPAdapter(list_object, context);
                manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                    @Override
                    public int getSpanSize(int position) {
                        if (adapter.getItemViewType(position) == adapter.GROUP) {
                            return 3;
                        }
                        return 1;
                    }
                });
                iCategoryDanPView.setAdapterSuccess(adapter, manager);
            }

            @Override
            public void getListViewDataSuccess(List<CategroyDanPBean.DataBean.CategoriesBean>
                                                       list_categories) {
                CategoryListViewAdapter adapter = new CategoryListViewAdapter(list_categories,
                        context);
                iCategoryDanPView.setListViewAdapterSuccess(adapter);
            }

            @Override
            public void getDataFailed(Throwable t) {
                iCategoryDanPView.setAdapterFailed(t);
            }
        });

    }
}
